package net.huazai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.huazai.entity.AddressAddEntity;
import net.huazai.entity.AddressEditEntity;
import net.huazai.entity.LoginUser;
import net.huazai.enums.AddressStatus;
import net.huazai.interceptor.LoginInterceptor;
import net.huazai.mapper.UserAddressMapper;
import net.huazai.model.UserAddressDO;
import net.huazai.service.UserAddressService;
import net.huazai.vo.UserAddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 11987
 */
@Service
@Slf4j
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    /**
     * 添加收货地址
     * @param addressAddEntity 收货地址对象
     */
    @Override
    public void add(AddressAddEntity addressAddEntity) {

        // 获取用户登录信息 这里还是通过 ThreadLocal 进行获取
        LoginUser loginUser = LoginInterceptor.LOGIN_USER_THREAD_LOCAL.get();

        UserAddressDO userAddressDO = new UserAddressDO();
        // 用户id
        userAddressDO.setUserId(loginUser.getId());
        userAddressDO.setCreateTime(new Date());
        userAddressDO.setUpdateTime(new Date());

        // 拷贝请求到 DO 对象中
        BeanUtils.copyProperties(addressAddEntity, userAddressDO);

        // 判断是否是默认的收货地址
        if (userAddressDO.getDefaultStatus() == 1) {
            // 这里需要查询是否有默认收货地址
            UserAddressDO  defaultUserAddressDO = userAddressMapper.selectOne(new QueryWrapper<UserAddressDO>()
                    .eq("user_id", loginUser.getId())
                    .eq("default_status", AddressStatus.DEFAULT_STATUS.getStatus()));
            if (defaultUserAddressDO != null) {
                // 设置为非默认
                defaultUserAddressDO.setDefaultStatus(AddressStatus.NORMAL_STATUS.getStatus());
                // 修改原先默认收货地址为非默认
                userAddressMapper.update(defaultUserAddressDO,
                        new QueryWrapper<UserAddressDO>().eq("id", defaultUserAddressDO.getId()));
                log.info("用户收货地址模块-新增收货地址：default={}", defaultUserAddressDO);
            }
        }

        // 新增收货地址
        int rows = userAddressMapper.insert(userAddressDO);
        log.info("用户收货地址模块-新增收货地址：rows={}，data={}", rows, userAddressDO);
    }

    @Override
    public UserAddressVO detail(Long addressId) {
        UserAddressDO userAddressDO = userAddressMapper.selectOne(new QueryWrapper<UserAddressDO>().eq("id", addressId));
        // 如果为空，则返回空
        if (userAddressDO == null) {
            return null;
        }

        UserAddressVO userAddressVO = new UserAddressVO();
        BeanUtils.copyProperties(userAddressDO, userAddressVO);
        return userAddressVO;
    }

    @Override
    public int del(Long addressId) {
        return userAddressMapper.delete(new QueryWrapper<UserAddressDO>().eq("id", addressId));
    }

    @Override
    public void edit(AddressEditEntity addressEditEntity) {
        // 获取用户登录信息 这里还是通过 ThreadLocal 进行获取
        LoginUser loginUser = LoginInterceptor.LOGIN_USER_THREAD_LOCAL.get();

        UserAddressDO userAddressDO = new UserAddressDO();
        // 用户id
        userAddressDO.setUserId(loginUser.getId());
        // 重新设置修改时间
        userAddressDO.setUpdateTime(new Date());

        // 拷贝请求到 DO 对象中
        BeanUtils.copyProperties(addressEditEntity, userAddressDO);

        // 判断是否是默认的收货地址
        if (userAddressDO.getDefaultStatus() == 1) {
            // 这里需要查询是否有默认收货地址
            UserAddressDO  defaultUserAddressDO = userAddressMapper.selectOne(new QueryWrapper<UserAddressDO>()
                    .eq("user_id", loginUser.getId())
                    .eq("default_status", AddressStatus.DEFAULT_STATUS.getStatus()));
            if (defaultUserAddressDO != null) {
                // 设置为非默认
                defaultUserAddressDO.setDefaultStatus(AddressStatus.NORMAL_STATUS.getStatus());
                // 修改原先默认收货地址为非默认
                userAddressMapper.update(defaultUserAddressDO,
                        new QueryWrapper<UserAddressDO>().eq("id", defaultUserAddressDO.getId()));
                log.info("用户收货地址模块-修改收货地址：default={}", defaultUserAddressDO);
            }
        }

        // 新增收货地址
        int rows = userAddressMapper.update(userAddressDO, new QueryWrapper<UserAddressDO>().eq("id", userAddressDO.getId()));
        log.info("用户收货地址模块-修改收货地址：rows={}，data={}", rows, userAddressDO);
    }

    @Override
    public List<UserAddressVO> findAllAddress() {
        LoginUser loginUser = LoginInterceptor.LOGIN_USER_THREAD_LOCAL.get();
        // 查找当前用户的所有的收货地址
        List<UserAddressDO> list = userAddressMapper.selectList(new QueryWrapper<UserAddressDO>().eq("user_id", loginUser.getId()));

        // 对象流式转换

        return list.stream().map(obj->{
            UserAddressVO userAddressVO = new UserAddressVO();
            BeanUtils.copyProperties(obj, userAddressVO);
            return userAddressVO;
        }).collect(Collectors.toList());
    }
}

